package com.yy.bolg.service.impl;

import com.yy.bolg.dao.AccessoryClassDao;
import com.yy.bolg.entity.AccessoryBean;
import com.yy.bolg.entity.AccessoryClassBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.AccessoryClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 附件分类Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-11-04
 */
@Service("AccessoryClassService")
public class AccessoryClassServiceImpl implements AccessoryClassService {

    @Autowired
    private AccessoryClassDao accessoryClassDao;

    @Override
    public void add(AccessoryClassBean bean) throws Exception {

        if(StringUtils.isEmpty(bean.getPsAccessoryClassCode())){
            throw new LackParameterException("附件分类编号不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsAccessoryClassName())){
            throw new LackParameterException("附件分类名称不能为空");
        }

        try {
            accessoryClassDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public void del(AccessoryClassBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAccessoryClassCode())) {
            throw new LackParameterException("附件分类编号不能为空");
        }

        if (null == accessoryClassDao.findOneByCode(bean.getPsAccessoryClassCode())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            accessoryClassDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }


    @Override
    public List<AccessoryClassBean> findAll() throws Exception {
        try {
            return accessoryClassDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public AccessoryClassBean findOneByCode(AccessoryClassBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAccessoryClassCode())) {
            throw new LackParameterException("附件分类编号不能为空");
        }

        try {
            return accessoryClassDao.findOneByCode(bean.getPsAccessoryClassCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }
}
